package com.zjt.myjiakao.dialog;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.OnOutsidePhotoTapListener;
import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.zjt.myjiakao.R;

import java.util.List;


/**
 * @author Admin
 */
public class ImgPagerAdapter extends PagerAdapter {

    List<String> imgList;
    Context mContext;
    ImgDialog dialog;
    public ImgPagerAdapter(Context context, List<String> imgList, ImgDialog dialog) {
        this.imgList = imgList;
        this.mContext = context;
        this.dialog = dialog;
    }

    @Override
    public int getCount() {
        return imgList.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = View.inflate(mContext, R.layout.item_show_img,null);
        final PhotoView photoView =  view.findViewById(R.id.photo_view);
        photoView.setOnPhotoTapListener(new OnPhotoTapListener() {
            @Override
            public void onPhotoTap(ImageView view1, float x, float y) {
                ImgPagerAdapter.this.dismiss();
            }
        });

        photoView.setOnOutsidePhotoTapListener(new OnOutsidePhotoTapListener() {
            @Override
            public void onOutsidePhotoTap(ImageView imageView) {
                ImgPagerAdapter.this.dismiss();
            }
        });
        Glide.with(mContext).load(imgList.get(position)).into(photoView);
        container.addView(view);
        return view;
    }


    private void dismiss(){
        if(dialog!=null){
            dialog.dismiss();
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
}
